package com.lhys.controller;

import com.github.pagehelper.PageInfo;
import com.lhys.common.ApiResultMsg;
import com.lhys.common.ResultInfo;
import com.lhys.controller.base.BaseController;
import com.lhys.dto.AssetUserDto;
import com.lhys.pojo.AssetUser;
import com.lhys.service.AssetUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Authorization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * Created with IntelliJ IDEA.
 * User: xiaoqi
 * Date: 2021-06-23
 * Time: 下午 20:31
 * Description: 资产用户控制器
 */
@Api(tags = "资产用户模块")
@RestController
@RequestMapping("/api/asset-user")
public class AssetUserController extends BaseController {

    @Autowired
    private AssetUserService assetUserService;

    @Value("${login.password-error}")
    private String passwordErrorInfo;

    @GetMapping("/list")
    @ApiOperation(value = "根据条件查询所有人员，进行分页", authorizations = {@Authorization(value = header)})
    public ApiResultMsg list(AssetUser assetUser, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageInfo<AssetUser> pageInfo = assetUserService.listAssetUserByPage(assetUser, pageNum, pageSize);
        return ApiResultMsg.success(ResultInfo.SUCCESS, pageInfo);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "根据id删除用户", authorizations = {@Authorization(value = header)})
    public ApiResultMsg delete(@PathVariable("id") Integer id) {
        Boolean result = assetUserService.delAssetUserById(id);
        if (result) {
            return ApiResultMsg.success(ResultInfo.SUCCESS);
        }
        return ApiResultMsg.error(ResultInfo.ERROR);
    }

    @PutMapping("/save")
    @ApiOperation(value = "保存用户", authorizations = {@Authorization(value = header)})
    public ApiResultMsg save(@RequestBody AssetUser assetUser) {
        Boolean result = assetUserService.saveAssetUser(assetUser);
        if (result) {
            return ApiResultMsg.success(ResultInfo.SUCCESS);
        }
        return ApiResultMsg.error(ResultInfo.ERROR);
    }

    @PostMapping("/update-password")
    @ApiOperation(value = "修改用户密码", authorizations = {@Authorization(value = header)})
    public ApiResultMsg updatePassword(@RequestBody AssetUserDto assetUserDto) {
        if (!assetUserDto.getNewPassword().equals(assetUserDto.getRePassword())) {
            return ApiResultMsg.error("两次密码不一致");
        }

        AssetUser assetUser = new AssetUser();
        assetUser.setLoginName(assetUserDto.getLoginName());
        assetUser.setPassword(assetUserDto.getOldPassword());
        AssetUser findUser = assetUserService.getAssetUserByLoginNameAndPassword(assetUser);
        if (findUser == null || findUser.getPassword().equals(passwordErrorInfo)) {
            return ApiResultMsg.error("原始密码不正确");
        }

        Boolean result = assetUserService.updatePasswordById(assetUserDto);
        if (result) {
            return ApiResultMsg.success("修改成功,请重新登录");
        }

        return ApiResultMsg.error("修改失败");
    }

}